﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using DevelopTool.Bll.LuckDrawBlls;
using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using DevelopTool.Model.LuckDrawModels;
using Sunny.UI;

namespace DevelopTool.Forms.LuckDrawForms
{
    /// <summary>
    /// 选择困难症
    /// </summary>
    public partial class LuckDrawForm : BaseUIPage
    {
        /// <summary>
        /// 抽奖对象
        /// </summary>
        private LuckDraw luckDraw;

        private LuckDrawBll luckDrawBll;

        private Timer timer;

        private List<UIStyle> styles = new List<UIStyle>();

        private bool runStatus = false;


        public LuckDrawForm(TreeNode treeNode, LuckDraw luckDraw)
        {
            InitializeComponent();

            this.luckDraw = luckDraw;
            this.luckDrawBll = new LuckDrawBll();
        }

        private void LuckDrawForm_Load(object sender, EventArgs e)
        {
            this.Disposed += LuckDrawForm_Disposed;

            foreach (UIStyle style in Enum.GetValues(typeof(UIStyle)))
                this.styles.Add(style);

            this.lblResult.Font = new Font("宋体", 70F);

            this.codeEditorControl.ContentText = luckDraw.Text;
            
            this.txtInterval.IntValue = luckDraw.Interval;
            this.chbRandomColor.Checked = luckDraw.RandomColor;
            this.upDownFontSize.Value = luckDraw.FontSize;
            this.lblResult.Font = new Font(this.lblResult.Font.FontFamily, upDownFontSize.Value);
        }


        private void LuckDrawForm_Disposed(object sender, EventArgs e)
        {
            Stop();

            if (timer != null)
            {
                timer.Stop();
                timer.Dispose();
            }
        }

        /// <summary>
        /// 自动保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void codeEditorControl_ContentTextChanged(object sender,EventArgs e)
        {
            Save();
        }

        /// <summary>
        /// 开始
        /// </summary>
        private void Start()
        {
            if (this.runStatus) return;

            //按行分隔抽奖内容
            List<string> list = this.codeEditorControl.ContentText.Split(new string[] { "\r\n" }, StringSplitOptions.None).Where(it => !string.IsNullOrWhiteSpace(it)).ToList();

            if (list.Count <= 0)
            {
                this.ShowWarningTip("请在右侧文本框输入内容");
                return;
            }

            this.btnStart.Text = "停止";
            this.btnStart.Symbol = 61517;
            this.runStatus = true;
            Save();

            //启动计时器
            this.timer = new Timer();
            timer.Interval = txtInterval.IntValue;
            timer.Enabled = true;
            timer.Tick += delegate (object ss, EventArgs ee)
            {
                if (this.chbRandomColor.Checked)//随机颜色
                {
                    var backColor = ColorHelper.GetRandomColor();
                    this.lblResult.BackColor = backColor;
                    this.uiSplitContainer1.Panel1.BackColor = backColor;
                }
                else
                {
                    this.lblResult.BackColor = Color.FromName("ffeef8f8");
                    this.uiSplitContainer1.Panel1.BackColor = Color.FromName("ffeef8f8");
                }

                this.lblResult.Text = list[RandomEx.Next(list.Count)];//随机逻辑，就一行代码，是不是很神奇~
                this.lblResult.Style = styles[RandomEx.Next(styles.Count)];
            };

        }


        /// <summary>
        /// 停止
        /// </summary>
        private void Stop()
        {
            if (!this.runStatus) return;

            this.btnStart.Text = "开始";
            this.btnStart.Symbol = 61515;
            this.runStatus = false;

            if (this.timer != null)
            {
                this.timer.Stop();
                this.timer.Dispose();
            }

            this.listBoxResult.Items.Add($"{this.listBoxResult.Items.Count + 1}.{this.lblResult.Text}");//抽中的结果显示到历史记录

            //绑定动画
            AnimatorHelper.BindShowAnimator(new List<Control>() {
                    { this.lblResult},
                });
        }


        /// <summary>
        /// 保存
        /// </summary>
        private void Save()
        {
            this.luckDraw.Text = this.codeEditorControl.ContentText;
            this.luckDraw.Interval = this.txtInterval.IntValue;
            this.luckDraw.RandomColor = this.chbRandomColor.Checked;
            this.luckDraw.FontSize = this.upDownFontSize.Value;
            this.luckDrawBll.Save(this.luckDraw);
        }


        private void btnStart_Click(object sender, EventArgs e)
        {
            if (this.runStatus)
                Stop();
            else
                Start();
        }

        private void lblResult_MouseClick(object sender, MouseEventArgs e)
        {
            if (this.runStatus)
                Stop();
            else
                Start();
        }


        private void 清空历史记录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.listBoxResult.Items.Clear();
        }

        private void 导出记录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.listBoxResult.Items.Count <= 0) return;

            string filter = "文本|*.txt";
            string fileName = string.Empty;

            if (!Dialogs.SaveFileDialog(ref fileName, filter)) return;
            if (string.IsNullOrEmpty(fileName)) return;

            File.WriteAllLines(fileName, this.listBoxResult.Items.Cast<string>().ToList());

            this.ShowSuccessNotifier($"已保存到文件【{fileName}】");
        }


        /// <summary>
        /// 循环速度调整
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtInterval_TextChanged(object sender, EventArgs e)
        {
            if (this.timer != null && this.timer.Enabled)
            {
                this.timer.Interval = txtInterval.IntValue;
            }
        }

        private void upDownFontSize_ValueChanged(object sender, int value)
        {
            this.lblResult.Font = new Font(this.lblResult.Font.FontFamily, upDownFontSize.Value);
        }

        
    }
}
